package com.ghsc.ghxc.jsb.ui.login

import androidx.lifecycle.MutableLiveData
import com.cn.appcore.base.BaseViewModel
import com.cn.appcore.ext.executeResponse
import com.cn.appcore.http.bean.BaseResponse
import com.ghsc.ghxc.jsb.api.ApiMarket
import com.ghsc.ghxc.jsb.ui.bean.SysConfigResult
import com.ghsc.ghxc.jsb.ui.login.bean.LoginResultData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.login
 * 类名：LoginViewModel
 * 创建时间：2024/4/22 11:09
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class LoginViewModel   : BaseViewModel() {


    /**  配置 **/
    val onSysConfigData by lazy { MutableLiveData<SysConfigResult>() }
    fun onSysConfigMethod(paras: String) {
        launch {
            val response = withContext(Dispatchers.IO) { ApiMarket.service.onSysConfigData(paras) }
            executeResponse(
                response,
                { onSysConfigData.value = response.data },
                {
                    againLoginMsg.value = response.msg
                },
            ) { errorMsg.value = response.msg }
        }
    }


    /**  登录 **/
    val loginData: MutableLiveData<BaseResponse<LoginResultData>> = MutableLiveData()
    fun onLoginMethod(paras: String) {
        launch {
            val response = withContext(Dispatchers.IO) { ApiMarket.service.onLoginData(paras) }
            executeResponse(
                response,
                { loginData.value = response },
                {
                    againLoginMsg.value = response.msg
                },
            ) { errorMsg.value = response.msg }
        }
    }


    /** 验证码 **/
    val onPhoneCodeData: MutableLiveData<BaseResponse<Any>> = MutableLiveData()
    fun onPhoneCodeMethod(paras: String) {
        launch {
            val response = withContext(Dispatchers.IO) { ApiMarket.service.onSendCodeData(paras) }
            executeResponse(
                response,
                { onPhoneCodeData.value = response},
                { againLoginMsg.value = response.msg}
            ) { errorMsg.value = response.msg }

        }
    }

    /** 注册 **/
    val onUserRegisterData: MutableLiveData<BaseResponse<LoginResultData>> = MutableLiveData()
    fun onUserRegisterMethod(paras: String) {
        launch {
            val response = withContext(Dispatchers.IO) {  ApiMarket.service.onUserRegisterData(paras) }
            executeResponse(
                response,
                { onUserRegisterData.value = response},
                { againLoginMsg.value = response.msg}
            ) { errorMsg.value = response.msg }

        }
    }

    /** 修改密码 **/
    val onSetUpPwdData: MutableLiveData<BaseResponse<Any>> = MutableLiveData()
    fun onSetUpPwdMethod(paras: String) {
        launch {
            val response = withContext(Dispatchers.IO) { ApiMarket.service.onSetUpPwdData(paras) }
            executeResponse(
                response,
                { onSetUpPwdData.value = response},
                { againLoginMsg.value = response.msg}
            ) { errorMsg.value = response.msg }

        }
    }
}